<?php


function mysort( $parentId, $indent, $arrSrc, &$arrDst )
{
	++$indent;
	$i = 0;
	for ( ;	$i < count( $arrSrc );	++$i	)
	{
		if ( $arrSrc[$i]['f_parentId'] == $parentId )
		{
			$arr = $arrSrc[$i];
			$arrDst[] = array(
				"f_id" => $arr['f_id'],
				"f_name" => $arr['f_name'],
				"f_parentId" => $arr['f_parentId'],
				"f_indent" => $indent
			);
			if ( $arr['f_haveChild'] )
			{
				mysort( $arr['f_id'], $indent, $arrSrc, &$arrDst );
			}
		}
	}
	--$indent;
}

require_once( "_inc.php" );
$arrSrc = $db->fetchrows( "SELECT f_id, f_parentId, f_name, f_haveChild FROM ".$gDb['prefix']."categories ORDER BY f_parentId ASC, f_order ASC, f_id ASC" );
$arrDst = array( );
$indent = 0;
mysort( 0, 0, $arrSrc, &$arrDst );
echo "/*<table border=1>";
$i = 0;
for ( ;	$i < count( $arrDst );	++$i	)
{
	$id = $arrDst[$i]['f_id'];
	$indent = $arrDst[$i]['f_indent'];
	$sort = $i + 1;
	$db->query( "UPDATE ".$gDb['prefix'].( "categories SET f_sort='".$sort."', f_indent='{$indent}' WHERE (f_id='{$id}')" ) );
	echo "<tr><td>".$i."</td><td>".$arrDst[$i]['f_id']."</td><td>".$arrDst[$i]['f_parentId']."</td><td>".$arrDst[$i]['f_indent']."</td><td>".$arrDst[$i]['f_name']."</td></tr>";
}
echo "</table>*/";
?>
